14个 Python 命令行冷兵器,一个比一个强大!
The following article is from 游戏不存在 Author 肖恩顿
Python自带了一些命令行工具,可以用来快速处理工作。我把这些命令行工具称之为 冷兵器 ,没有趁手工具时候可以顶替使用。这些工具都是python标准模块,具有 main 函数,直接使用 python -m
命令执行,多数可以使用 -h/--help
查看帮助。
1. http 服务
启动一个http静态文件服务
# python -m http.server
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
2. json 格式化
格式化json数据
# echo '{"amount":3.4}' | python -m json.tool
{
"amount": 3.4
}
3. 可视化编辑器和shell
idlelib模块基于tkinter,可以作为编辑器和shell使用。文件编辑器效果。
python -m idlelib myapp/example.py
shell效果
python -m idlelib
4. python 应用程序打包
构建myapp目录如下:
├── myapp
│ ├── example.py
│ └── hello.py
代码分别如下:
# example.py
import hello
def main():
print('Hello World')
hello.say_hello("python")
if __name__=='__main__':
main()
# hello.py
def say_hello(name):
print("hello",name)
将整个myapp打包成应用程序,命令执行后会生成一个名叫 myapp.pyz 应用程序。
python -m zipapp myapp -m "example:main"
使用python直接运行应用程序
# python myapp.pyz
Hello World
hello python
5. ROT13 加密
rot13(rotate by 13 places)是一种简易的替换式密码,是凯撒密码的变种。rot13将原文偏移13位形成密文,因为英文总共26位,所以密文再偏移13位后会回到原文。公式: rot13(rot13(xxx))=xxx
。
# echo "xxx" | python -m encodings.rot_13
Tvir zr n fgne
作为一个小彩蛋,欢迎大家动手破译一下xxx的值
6. base64 编码
对一个字符串进行base64编码
# echo "haha" | python -m base64
aGFoYQo=
# echo "aGFoYQo=" | python -m base64 -d
haha
base64还支持对文件编码。编写测试代码
# sample.py
def main():
print('Hello World👌')
if __name__=='__main__':
main()
将代码编译成base64字符串
# python -m base64 sample.py
CmRlZiBtYWluKCk6CiAgIHByaW50KCdIZWxsbyBXb3JsZPCfkYwnKQogICAKaWYgX19uYW1lX189
PSdfX21haW5fXyc6CiAgIG1haW4oKQo=
执行编译后的代码
# echo "CmRlZiBtYWluKCk6CiAgIHByaW50KCdIZWxsbyBXb3JsZPCfkYwnKQogICAKaWYgX19uYW1lX189
PSdfX21haW5fXyc6CiAgIG1haW4oKQo=" | python -m base64 -d | python
Hello World👌
类似的可以使用uu(Unix-to-Unix encoding)编码代码:
# python -m uu sample.py
begin 644 sample.py
M"F1E9B!M86EN*"DZ"B @('!R:6YT*"=(96QL;R!7;W)L9/"?D8PG*0H@(" *
C:68@7U]N86UE7U\]/2=?7VUA:6Y?7R<Z"B @(&UA:6XH*0H
end
使用quopri(Encode and decode MIME quoted-printable data)编码代码:
# python -m quopri -t sample.py
def=20main():
=20=20=20print('Hello=20World=F0=9F=91=8C')
=20=20=20
if=20__name__=3D=3D'__main__':
=20=20=20main()
7. mime 识别
识别文件或者url的mime类型
# python -m mimetypes https://docs.python.org/3/library/mimetypes.html
type: text/html encoding: None # html
# python -m mimetypes https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png
type: image/png encoding: None # png
# python -m mimetypes sample.py
type: text/x-python encoding: None # python文件
# python -m mimetypes sample.py.gz
type: text/x-python encoding: gzip # python文件,gzip压缩
8. 查看python环境信息
python -m sysconfig
Platform: "macosx-10.9-x86_64"
Python version: "3.8"
Current installation scheme: "posix_prefix"
Paths:
data = "/Users/yoo/work/yuanmahui/python/.venv"
include = "/Library/Frameworks/Python.framework/Versions/3.8/include/python3.8"
...
Variables:
...
PYTHON = "python"
PYTHONFRAMEWORK = "Python"
PYTHONFRAMEWORKDIR = "Python.framework"
PYTHONFRAMEWORKINSTALLDIR = "/Library/Frameworks/Python.framework"
...
也可以使用下面命令查看系统路径
# python -m site
sys.path = [
'/Users/yoo/work/yuanmahui/python/python-tools',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload',
'/Users/yoo/work/yuanmahui/python/.venv/lib/python3.8/site-packages',
]
USER_BASE: '/Users/yoo/Library/Python/3.8' (exists)
USER_SITE: '/Users/yoo/Library/Python/3.8/lib/python/site-packages' (exists)
ENABLE_USER_SITE: False
9. 编译脚本
compileall可以编译python脚本。myapp目录有2个py脚本
# ll myapp
total 16
-rw-r--r-- 1 yoo staff 118B 2 26 23:03 example.py
-rw-r--r-- 1 yoo staff 43B 2 26 23:03 hello.py
编译脚本
# python -m compileall myapp
Listing 'myapp'...
Compiling 'myapp/example.py'...
Compiling 'myapp/hello.py'...
查看编译结果
tree myapp -L 3
myapp
├── __pycache__
│ ├── example.cpython-38.pyc
│ └── hello.cpython-38.pyc
├── example.py
└── hello.py
1 directory, 4 files
10. 压缩和解压
创建和解压tar包
# python -m tarfile -c myapp.tar myapp # 创建myapp.tar 压缩包
# python -m tarfile -e myapp.tar myapp2 # 解压myapp.tar 到 myapp2目录
使用gzip压缩文件
# python -m gizp sample.py
# python -m gzip -d sample.py.gz
使用zip打包文件
# python -m zipfile -c myapp.zip myapp
# python -m zipfile -e myapp.zip myapp2
注意: zipfile和zipapp不一样, 后则是生成一个可以执行的app
11. telnet 工具
没有telnet工具的python容器中可以这样:
# python -m telnetlib -d redis 6379 # 连接redis
monitor
Telnet(redis,6379): send b'monitor\n'
Telnet(redis,6379): recv b'-NOAUTH Authentication required.\r\n'
-NOAUTH Authentication required.
还有 nntplib && ftplib 两个工具,应该很少使用,就不介绍了
12. 性能和调试工具
自带的timeit可以测试脚本的性能数据
# python -m timeit '"-".join([str(n) for n in range(100) if n%2 == 0])' # 取模求偶数
20000 loops, best of 5: 12.5 usec per loop
# python -m timeit '"-".join([str(n) for n in range(0,100,2)])' # 步进
50000 loops, best of 5: 8.85 usec per loop
# python -m timeit '"-".join([str(n) for n in range(100) if n&1 == 0])' # 位运算判断奇偶
20000 loops, best of 5: 14.3 usec per loop
不科学1: 位运算竟然比取模慢?
如果不是一个字符串而是一个脚本:
def test_normal():
tmp = ""
for x in range(100):
if x % 2 == 0:
if tmp:
tmp = tmp+"-"+str(x)
else:
tmp = str(x)
return tmp
if __name__ == '__main__':
print(test_normal())
可以下面方式变通使用
# python -m base64 test_string_join.py | python -m base64 -d | python -m timeit
50000000 loops, best of 5: 5.33 nsec per loop
不科学2: 50000000循环只需要5.33纳秒,而之前的用例20000循环需要12.5微秒
标准库中还有 pdb && profile && pstats 使用比较复杂,暂时就不介绍
pypy3 -m timeit '[{} for n in range(1000)]'
WARNING: timeit is a very unreliable tool. use pyperf or something else for real measurements
pypy3 -m pip install pyperf
pypy3 -m pyperf timeit '[{} for n in range(1000)]'
------------------------------------------------------------
100000 loops, average of 7: 7.3 +- 0.107 usec per loop (using standard deviation)
[tywork@liujunhong_szfsfz_work1 config]$ pypy3 -m pyperf timeit '[{} for n in range(1000)]'
........
Mean +- std dev: 8.42 us +- 0.25 us
[tywork@liujunhong_szfsfz_work1 config]$
[tywork@liujunhong_szfsfz_work1 config]$
[tywork@liujunhong_szfsfz_work1 config]$
[tywork@liujunhong_szfsfz_work1 config]$ pypy3 -m pyperf timeit '[dict() for n in range(1000)]'
.........
Mean +- std dev: 29.6 us +- 1.1 us
13. pydoc
本地服务方式查看python代码文档
# python -m pydoc -p 8080 # 启动一个web服务
Server ready at http://localhost:8080/
Server commands: [b]rowser, [q]uit
server> b
文档效果如图
14. test
执行python自带的测试用例,查看系统支持那些特性
# python -m test.regrtest -u cpu
== CPython 2.7.16 (default, Jun 5 2020, 22:59:21) [GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc-
== Darwin-19.6.0-x86_64-i386-64bit little-endian
== /private/var/folders/mv/3vgd3mdx2453clfcst7qlm280000gn/T/test_python_13470
== CPU count: 12
Run tests sequentially
0:00:00 load avg: 2.53 [ 1/404] test_grammar
...
= Tests result: FAILURE ==
363 tests OK.
5 tests failed:
test_import test_posix test_py_compile test_rlcompleter
test_scriptpackages
36 tests skipped:
test_al test_bsddb test_bsddb3 test_cd test_cl test_codecmaps_cn
test_codecmaps_hk test_codecmaps_jp test_codecmaps_kr
test_codecmaps_tw test_curses test_epoll test_gdb test_gdbm
test_gl test_imgfile test_largefile test_linuxaudiodev test_msilib
test_ossaudiodev test_poll test_py3kwarn test_smtpnet
test_socketserver test_spwd test_startfile test_sunaudiodev
test_timeout test_tk test_tools test_ttk_guionly test_urllib2net
test_urllibnet test_winreg test_winsound test_zipfile64
2 skips unexpected on darwin:
test_spwd test_tools
Total duration: 5 min 23 sec
Tests result: FAILURE
从测试用例,可以看到osx支持fork,不支持epool和poll。
0:00:47 load avg: 1.79 [138/404] test_fork1
...
0:00:39 load avg: 1.59 [125/404] test_epoll
test_epoll skipped -- test works only on Linux 2.6
...
0:02:42 load avg: 2.41 [257/404/1] test_poll
test_poll skipped -- select.poll not defined -- skipping test_poll
0:02:42 load avg: 2.41 [258/404/1] test_popen -- test_poll skipped
参考链接
PEP441--Improving Python ZIP Application Support https://www.python.org/dev/peps/pep-0441/ ROT13 https://zh.wikipedia.org/wiki/ROT13
- EOF -
觉得本文对你有帮助?请分享给更多人
推荐关注「Python开发者」,提升Python技能
点赞和在看就是最大的支持❤️